// News fetching YQL query
var szNewsFeed = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20rss%20where%20url%3D%22http%3A%2F%2Frss.news.yahoo.com%2Frss%2Ftopstories%22%20&format=json&diagnostics=true";

var feedCount = 0;

// same method, but using gadgets.io.makerequest
function readNewsURL(szURL, callBack, divid) {
	try {
		var params = {};
		params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON;
		gadgets.io.makeRequest(szURL, function(response) {
			feedCount = response.data.query.count;
			getNewsFromJSON(response, divid)
		}, params);

	} catch (ex) {
		handleException("readURL()" + ex.description);
	}
}

function getNewsFeed(divid) {
	try {
		feedCount = 0;
		// 1. generate URL based on user name
		var URL = szNewsFeed;
		// 2. read URL output
		// 3. consolidate url output into a single var (JSON object)
		readNewsURL(URL, getNewsFromJSON, divid);
		alert(feedCount);

		gadgets.window.adjustHeight(feedCount * 90);
	} catch (ex) {
		handleException("getNewsFeed()" + ex.description);
	}
}

// readURL will call this function back, with JSON object containing news feeds
function getNewsFromJSON(oJSON, divid) {
	try {
		showMessage("in getNewsFromJSON CallBack()");

		if (oJSON != null) {
			showMessage(oJSON.data.query.count);

			var szEntry = "";
			for ( var i = 0; i < oJSON.data.query.count; i++) {
				try {
					szEntry = oJSON.data.query.results.item[i].description;
					document.getElementById(divid).innerHTML += szEntry;
				} catch (ex) {
					handleMessage("getTwitFromJSON() : loop: " + ex.description);
				}
			}
		}
	} catch (ex) {
		handleMessage("getNewsFromJSON() : " + ex.description);
	}

}